<template>
    <el-drawer title="我是标题" :visible.sync="dialogVisible" :with-header="true" class="demand_box" size='600px' @close="resetForm('addPictureSingleSzForm')">
        <div class="flexbox flexcolumn height_100">
            <div class="demand_head flexbox flexcenter flexbetween">
                <span class="demand_title">创建单个需求</span>
                <span class="icon_btn icon_btn_32" @click="resetForm('addPictureSingleSzForm')"><img src="../../assets/img/close_check.svg" alt=""></span>
            </div>
            <div class="demand_center flex">
                <el-form label-position="top" style="margin-top:-10px" :model="addPictureSingleSzForm" size="medium" :hide-required-asterisk='true' ref="addPictureSingleSzForm" :rules="addPictureSingleSzRules" class="dialog_form">
                    <el-form-item label="品牌/产品" prop="">
                        <el-row :gutter="12">
                            <el-col :span="12">
                                <el-form-item prop="brand_id">
                                    <el-select v-model="brandSelect" placeholder="请选择品牌" value-key='brand_id' style="width:100%" @change="selectBrand">
                                        <el-option :label="item.brand_name+filters.chanelName(item.channel)" :value="item" v-for="item in brandList" :key="item.brand_id">
                                            <span>{{item.brand_name}}{{filters.chanelName(item.channel)}}</span>
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item prop="product_id">
                                    <el-select v-model="addPictureSingleSzForm.product_id" placeholder="请选择产品" style="width:100%" :disabled="!addPictureSingleSzForm.brand_id" @change="selectProduct">
                                        <el-option :label="item.name" :value="item.product_id" v-for="item in productList" :key="item.product_id"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form-item>
                    <el-form-item label="业务场景" prop="business_scene">
                        <el-select v-model="addPictureSingleSzForm.business_scene" :disabled="!addPictureSingleSzForm.product_id" placeholder="请选择业务场景" style="width:100%">
                            <el-option :label="item.scene_name" :value="item.business_scene" v-for="item in sceneList" :key="item.business_scene"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="平面" prop="designer_uid">
                        <el-select v-model="addPictureSingleSzForm.designer_uid" placeholder="请选择平面" style="width:100%">
                            <el-option :label="item.nickname" :value="item.id" v-for="item in optimiList" :key="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="截止日期" prop="closing_date">
                        <el-select v-model="addPictureSingleSzForm.closing_date" placeholder="请选择该需求的截止日期" style="width:100%">
                            <el-option label="周一" :value="1"></el-option>
                            <el-option label="周二" :value="2"></el-option>
                            <el-option label="周三" :value="3"></el-option>
                            <el-option label="周四" :value="4"></el-option>
                            <el-option label="周五" :value="5"></el-option>
                            <el-option label="周六" :value="6"></el-option>
                            <el-option label="周日" :value="7"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="需求信息" prop="description">
                        <el-input type="textarea" :rows="3" placeholder="请输入需求信息" v-model="addPictureSingleSzForm.description">
                        </el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div class="demand_footer flexbox flexcenter">
                <el-button plain size="small" class="el-button-width" @click="resetForm('addPictureSingleSzForm')">取 消</el-button>
                <el-button type="primary" size="small" class="el-button-width" @click="submitForm('addPictureSingleSzForm')" :loading="loading" :disabled='loading'>完成创建</el-button>
            </div>
        </div>
    </el-drawer>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            brandSelect: {},
            brandList: [], //品牌列表
            productList: [], //产品列表
            optimiList: [], //优化师列表
            sceneList: [], //业务场景列表
            scriptList: [],
            addPictureSingleSzForm: {
                year: "",
                week: "",
                brand_id: "",
                product_id: "",
                channel: "",
                business_scene: "",
                designer_uid: "",
                closing_date: "",
                description: "",
            },
            addPictureSingleSzRules: {
                brand_id: [
                    {
                        required: true,
                        message: "请选择品牌",
                        trigger: "change",
                    },
                ],
                product_id: [
                    {
                        required: true,
                        message: "请选择产品",
                        trigger: "change",
                    },
                ],
                business_scene: [
                    {
                        required: true,
                        message: "请选择业务场景",
                        trigger: "change",
                    },
                ],
                designer_uid: [
                    {
                        required: true,
                        message: "请选择平面",
                        trigger: "change",
                    },
                ],
                closing_date: [
                    {
                        required: true,
                        message: "请选择截止日期",
                        trigger: "change",
                    },
                ],
                description: [
                    {
                        required: true,
                        message: "请输入需求信息",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    created() {
        this.$bus.on("addPictureSingleSz", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.addPictureSingleSzForm.week = value.week;
            this.addPictureSingleSzForm.year = value.year;
            this.getBrandList();
            this.getOptimization();
        });
    },
    beforeDestroy() {
        this.$bus.off("addPictureSingleSz");
    },
    methods: {
        //获取品牌列表
        async getBrandList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/brandList"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.brandList = res.data;
            }
        },

        // 获取产品
        async getProductList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/productList",
                {
                    brand_id: this.addPictureSingleSzForm.brand_id,
                }
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.productList = res.data;
            }
        },

        // 获取业务场景
        async getSceneList() {
            const { data: res } = await this.$http.post(
                "adminapi/project/sceneList",
                {
                    brand_id: this.addPictureSingleSzForm.brand_id,
                    product_id: this.addPictureSingleSzForm.product_id,
                }
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.sceneList = res.data;
            }
        },

        // 获取优化师列表
        async getOptimization() {
            const { data: res } = await this.$http.post(
                "adminapi/company/szGraphicList"
            );
            if (res.code !== 200) {
                this.$message.error(res.msg);
                return;
            } else {
                this.optimiList = res.data;
            }
        },

        //品牌选择
        selectBrand() {
            this.addPictureSingleSzForm.brand_id = this.brandSelect.brand_id;
            this.addPictureSingleSzForm.channel = this.brandSelect.channel;
            this.addPictureSingleSzForm.product_id = "";
            this.addPictureSingleSzForm.business_scene = "";
            this.getProductList();
        },

        // 产品选择
        selectProduct() {
            this.addPictureSingleSzForm.business_scene = "";
            this.getSceneList();
        },

        // 提交
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "adminapi/demandsz/pictureAdd",
                        this.addPictureSingleSzForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "创建成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.resetForm(formName);
                        this.$parent.getDataList(1);
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
            this.brandSelect = {};
        },
    },
    components: {},
};
</script>
<style scoped>
/deep/ .el-drawer__header{
    height: 0;
    width: 0;
    padding: 0;
    margin: 0;
    overflow: hidden;
}
</style>


